/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package admin;

/**
 *
 * @author cengiz
 */
import java.io.*;
  import java.net.*;
  import java.awt.BorderLayout;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
  import javax.swing.*;

  public class AppletClient extends JApplet {
    // Label for displaying the visit count
    private JLabel jlblCount = new JLabel();

    // Indicate if it runs as application
    private boolean isStandAlone = false;

    // Host name or ip
    private String host = "localhost";

    /** Initialize the applet */
    public void init() {
    
     try {
// Create a server socket
        ServerSocket serverSocket = new ServerSocket(8000);
         System.out.println("Server started at " + new Date() + '\n');

        // Listen for a connection request
        Socket socketblue = serverSocket.accept();
        System.out.println("Blue connected at " + new Date() + '\n');
        ObjectOutputStream outputToClientblue = new ObjectOutputStream(socketblue.getOutputStream());
        ObjectInputStream inputFromClientblue = new ObjectInputStream(socketblue.getInputStream());
        Socket socketred = serverSocket.accept();
        System.out.println("Red connected at " + new Date() + '\n');
        ObjectOutputStream outputToClientred = new ObjectOutputStream(socketred.getOutputStream());
        ObjectInputStream inputFromClientred = new ObjectInputStream(socketred.getInputStream());

        gameInterface board = new gameInterface();
        while (true) {
        
            board.team = 0;
     
            //send status to blue
            outputToClientblue.writeObject(board);
            System.out.println("istemciye bilgi gonderiliyor....");
            System.out.println(board);
            try {
                //get move of the blue              
                board = (gameInterface) inputFromClientblue.readObject();
//test eğer illegal hareket var ise board.status = 0 yap outputToClientblue.writeObject(board); çağır ve sonlandır.Kazanan red team dır.

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AppletClient.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.err.println("Blue oynar"+board.ba1_y);
            
            //send status to blue
             board.team = 1;
             outputToClientred.writeObject(board);
            try {
                //get move of the blue
               
                 board = (gameInterface) inputFromClientred.readObject();
//test eğer illegal hareket var ise board.status = 0 yap outputToClientred.writeObject(board); çağır ve sonlandır.Kazanan blue team dır.
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AppletClient.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.err.println("Red oynar"+board.ra1_y);
        

        }
      }
      catch(IOException ex) {
        System.err.println(ex);
      }
    }


    
 
     public static void main(String[] args) {
        JFrame frame = new JFrame("Applet client");

      // Create an instance of the applet
      AppletClient applet = new AppletClient();
      applet.isStandAlone = true;
      // Get host
      if (args.length == 1) applet.host = args[0];

      // Add the applet instance to the frame
      frame.getContentPane().add(applet, BorderLayout.CENTER);

      // Invoke init() and start()
      applet.init();
      applet.start();

      // Display the frame
      frame.pack();
      frame.setVisible(true);
    }
    
  }
    /** Run the applet as an application */

